#coding=utf-8
from stutils import *
set_django_environ()
from stdj.stock.models import *
from stindex.indexdb import get_exdayslist
import datetime
td = datetime.date.today().isoformat()

name20 = 'gtetop20'
name55 = 'gtetop50'
def is_gtendtop(ed, n):
    '''当日收盘价是否大于等于过去n天最高价'''
    h = ed.highest(n)
    if not h:return False
    p = ed.price
    if p.end >= h:
        return True
    return False

def get_top(con, exdate, n=20):
    name = 'gte%idtop'%n
    
    c = con.cursor()
    c.execute('select code from selected where date=? and strategy=?',(exdate, name))
    r = c.fetchall()
    
    if not r:return None
    return [i[0] for i in r]

if __name__ == '__main__':
    from strategy import *
    con = get_con()    
    eds = ['2008-01-16',]
    
    for ed in eds:
        print ed
        for s in Share.objects.order_by('code'):
            try:
                e = s.exday_set.get(date = ed)
            except Exday.DoesNotExist, ex:
                continue
        
            if is_gtendtop(e, 20):
                selected2db(con, e, name20)
            
    